import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _Modal from "choerodon-ui/pro/lib/modal";

/**
 * 文件切片上传
 * @creationDate 2019/12/26
 */
import * as React from 'react';
import { Modal as CpaasModal } from 'antd';
import intl from 'utils/intl';
import { BKT_PUBLIC, CPAAS_FILE } from 'utils/config';
import { getAccessToken } from 'utils/utils';

var modalKey = _Modal.key();

var btnDisabledStyle = {
  cursor: 'not-allowed'
};
var btnStyle = {
  cursor: 'pointer'
};

var FileChunkUploader = function FileChunkUploader(props) {
  var _React$useState = React.useState(false),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      cpaasModalVisible = _React$useState2[0],
      setCpaasModalVisible = _React$useState2[1];

  var type = props.type,
      componentType = props.componentType,
      title = props.title,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      children = props.children,
      style = props.style,
      organizationId = props.organizationId,
      modalProps = props.modalProps;
  var accessToken = getAccessToken();
  var iframeUrl = React.useMemo(function () {
    var configure = {
      isBucket: false,
      href: '',
      search: ''
    };

    if (props.type === 'bucket') {
      configure.isBucket = true;
      configure.href = "".concat(CPAAS_FILE, "/v1/").concat(organizationId, "/upload");
      var searchObject = {
        access_token: accessToken,
        bucketName: props.bucketName || BKT_PUBLIC,
        directory: props.directory,
        storageCode: props.storageCode
      };
      configure.search = Object.keys(searchObject).filter(function (searchKey) {
        return searchObject[searchKey] !== undefined;
      }).map(function (searchKey) {
        return "".concat(searchKey, "=").concat(searchObject[searchKey]);
      }).join('&');
    } else {
      configure.isBucket = false;
      configure.href = "".concat(CPAAS_FILE, "/v1/").concat(organizationId, "/upload");
      var _searchObject = {
        access_token: accessToken,
        path: props.path,
        configCode: props.configCode
      };
      configure.search = Object.keys(_searchObject).filter(function (searchKey) {
        return _searchObject[searchKey] !== undefined;
      }).map(function (searchKey) {
        return "".concat(searchKey, "=").concat(_searchObject[searchKey]);
      }).join('&');
    }

    return "".concat(configure.href, "?").concat(configure.search);
  }, [CPAAS_FILE, organizationId, type, accessToken, JSON.stringify(props)]);
  var wrapStyle = React.useMemo(function () {
    return _objectSpread({
      height: '100%',
      width: '100%',
      border: 0
    }, style);
  }, [style]);
  var handleFileChunkUploaderClick = React.useCallback(function () {
    if (disabled) {
      return;
    }

    if (componentType === 'c7n') {
      _Modal.open(_objectSpread(_objectSpread({
        key: modalKey,
        drawer: true
      }, modalProps), {}, {
        children: /*#__PURE__*/React.createElement("iframe", {
          style: wrapStyle,
          src: iframeUrl,
          title: title
        })
      }));
    } else {
      setCpaasModalVisible(true);
    }
  }, [iframeUrl, disabled, componentType, setCpaasModalVisible, wrapStyle]);
  var closeCpaasModal = React.useCallback(function () {
    setCpaasModalVisible(false);
  }, [setCpaasModalVisible]);
  return /*#__PURE__*/React.createElement("div", null, React.isValidElement(children) ? React.cloneElement(children, {
    onClick: handleFileChunkUploaderClick
  }) : /*#__PURE__*/React.createElement("a", {
    onClick: handleFileChunkUploaderClick,
    style: disabled ? btnDisabledStyle : btnStyle
  }, intl.get('cfile.component.fileChunkUploader.view.upload').d('上传文件')), componentType === 'cpaas' && cpaasModalVisible && /*#__PURE__*/React.createElement(CpaasModal, Object.assign({
    visible: true,
    destroyOnClose: true,
    title: title,
    wrapClassName: "ant-modal-sidebar-right",
    transitionName: "move-right"
  }, modalProps, {
    onCancel: closeCpaasModal,
    onOk: closeCpaasModal
  }), /*#__PURE__*/React.createElement("iframe", {
    style: wrapStyle,
    src: iframeUrl,
    title: title
  })));
};

export default FileChunkUploader;